package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.auto.ArticleInfoMapper;
import com.model.auto.ArticleInfo;
import com.service.IArticleInfoService;
import com.util.Page;
import com.util.R;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 政策文章信息表 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@Service
public class ArticleInfoServiceImpl extends ServiceImpl<ArticleInfoMapper, ArticleInfo> implements IArticleInfoService {
    @Override
    public R getzc(Page page, String title, String type) {
        PageInfo<Object> pageInfo = PageHelper.startPage(page.getPageNum(), page.getPageSize())
                .doSelectPageInfo(() -> {
                    List<ArticleInfo> list = this.list(new LambdaQueryWrapper<ArticleInfo>()
                            //title不为空时添加查询条件
                            .like(title != null && !"".equals(title), ArticleInfo::getTitle, title)
                            .eq(type != null && !"".equals(type), ArticleInfo::getType, type)
                            .orderByAsc(ArticleInfo::getSort)
                    );
                });

        return R.ok(pageInfo);
    }

    /**
     * 阅读文章
     * @param id
     * @return
     */
    @Override
    public R read(Integer id) {
        baseMapper.updateReadCount(id);
        return R.ok();
    }

    @Override
    public R getFollow(String title, String type, Integer followType) {
        List<ArticleInfo> list = this.list(new LambdaQueryWrapper<ArticleInfo>()
                //title不为空时添加查询条件
                .like(title != null && !"".equals(title), ArticleInfo::getTitle, title)
                .eq(type != null && !"".equals(type), ArticleInfo::getType, type)
                .eq(Objects.nonNull(followType), ArticleInfo::getFollowType, followType)
                .orderByAsc(ArticleInfo::getSort)
        );

        return R.ok(list);
    }
}
